package com.qianya.system.face.web.patient.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianya.model.common.CommonResult;
import com.qianya.model.common.JWTUtil;
import com.qianya.model.common.ResultCode;
import com.qianya.model.pojo.patient.qo.PatientHisPrescriptionQO;
import com.qianya.model.pojo.patient.vo.MonthVo;
import com.qianya.system.face.web.patient.mapper.PatientHisPrescriptionMapper;
import com.qianya.system.face.web.patient.mapper.PatientStatisticsMapper;
import com.qianya.system.face.web.patient.model.PatientBoxRelevance;
import com.qianya.system.face.web.patient.model.PatientFileInfo;
import com.qianya.system.face.web.patient.model.PatientHisPrescription;
import com.qianya.system.face.web.patient.service.IPatientBoxRelevanceService;
import com.qianya.system.face.web.patient.service.IPatientFileInfoService;
import com.qianya.system.face.web.patient.service.IPatientHisPrescriptionService;
import com.qianya.system.face.web.patient.service.IPatientStatisticsService;
import com.qianya.util.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
* <p>
*  服药日志表服务实现类
* </p>
*
* @author lhl
* @since 2020-07-07
*/
@Service
public class PatientStatisticsServiceImpl implements IPatientStatisticsService{
    @Resource
    private PatientStatisticsMapper patientStatisticsMapper;
    @Resource
    private PatientHisPrescriptionMapper hisPrescriptionMapper;
    /**
     * @desc 患者用药统计
     * @author lhl
     * @date 2020-08-11 10:38
     * @return 
    */
    public CommonResult eatStatistics(String time){
        List<String> list=this.dateChange(time);
        List<MonthVo> patientStatic = hisPrescriptionMapper.eatStatistics(list.get(0), list.get(1));
        return CommonResult.success(DateUtils.listChangeMapReturn(patientStatic,list.get(0), list.get(1))) ;
    }
    /**
     * @desc 所有统计
     * @author lhl
     * @date 2020-08-11 10:38
     * @return
    */
    public CommonResult nursePharmacyStatistics(String time,String userId,Integer type){
        List<String> list=this.dateChange(time);
        List<MonthVo> patientStatic= hisPrescriptionMapper.nursePharmacyStatistics(list.get(0), list.get(1),userId,type);
        return CommonResult.success(DateUtils.listChangeMapReturn(patientStatic,list.get(0), list.get(1))) ;
        //return  CommonResult.success(patientStatic);
    }

    /**
     * @desc 根据时间获得之前的12个月份
     * @author lhl
     * @date 2020-08-11 10:37
     * @return
    */
    public List<String> dateChange(String time){
        List<String> list=new ArrayList<String>();
        Date date =null;
        if ("".equals(time)){
            date=new Date();
        }else {
            date=DateUtil.parse(time);
        }
        Calendar end = Calendar.getInstance();
        end.setTime(date);
        end.add(Calendar.DAY_OF_MONTH, 1);
        Calendar begin = Calendar.getInstance();
        BeanUtils.copyProperties(end,begin);
        begin.add(Calendar.YEAR, -1);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-01");
        list.add(sdf.format(begin.getTime()));
        list.add(sdf.format(end.getTime()));
        return list;
    }
}
